import { getSetting, chooseAddress, openSetting, showModal, showToast, requestPayment } from "../../utils/asyncWx.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
import { request } from "../../request/index.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders:{},
    tabs:[
      {
        id:0,
        value:"全部",
        isActive:true

      },
      {
        id:1,
        value:"待付款",
        isActive:false

      },
      {
        id:2,
        value:"代发货",
        isActive:false

      },
      {
        id:3,
        value:"退货/退款",
        isActive:false

      }
      
    ],

  },
  onShow(options){
    // 判断是否有token
    const token = wx.getStorageSync("token");
    if (!token) {
      wx.navigateTo({
        url: '/pages/auth/auth'
      });
      return;
    }
    
    let pages =getCurrentPages();
    
    let currentpage =pages[pages.length-1]
    const {type} = currentpage.options;
    this.changeTitleByIndex(type-1);
    this.getOrder(type)

  },
  //获取订单列表的方法
   async getOrder(type){
    const token =  wx.getStorageSync("token");
    const header = { Authorization: token };
    const res = await request({url:"/my/orders/all",header,data:{type}})
  
    this.setData({orders:res.data.message.orders.map(v=>({...v,create_time_cn:(new Date(v.create_time*1000).toLocaleString())}))})
  },
  // 根据标题索引来激活选中 标题数组
  changeTitleByIndex(index) {
    // 2 修改源数组
    let { tabs } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
    // 3 赋值到data中
    this.setData({
      tabs
    })
  },
   //点击事件
   handleTabsItemChange(e){
    //  获取被点击的索引
   const {index} = e.detail;
   this.changeTitleByIndex(index);
   this.getOrder(index+1)
    }
})